<template>
  <div class="app-container" style="height: 100%">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto" :class="{ disabled: disableAction }">
      <el-form-item prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目" filterable clearable :loading="prjLoading" style="width: 200px">
          <el-option v-for="project in projectList" :key="project.fid" :label="project.cflastname" :value="project.fid" />
        </el-select>
      </el-form-item>
      <el-form-item prop="routeName">
        <el-input v-model.trim="queryParams.routeName" placeholder="请输入路线名称" clearable @keyup.enter.native="handleQuery" style="width: 200px" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <template v-if="showBtns.add">
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="openAddItem" v-if="queryParams.projectId">新增</el-button>
          <el-popover placement="top" trigger="hover" content="请点击选择左侧项目" v-else>
            <el-button slot="reference" size="mini" icon="el-icon-plus" style="margin-left: 10px">新增</el-button>
          </el-popover>
        </template>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100% - 100px)">
      <el-table-column label="路线名称" prop="routeName" show-overflow-tooltip min-width="200" />
      <el-table-column label="点位数量" min-width="100">
        <span class="pointer" slot-scope="{ row }" @click="openSpotList">
          共
          <el-link type="primary" style="line-height: 1">{{ row.spotNum }}</el-link>
          个点位
        </span>
      </el-table-column>
      <el-table-column label="是否按顺序巡更" align="center" min-width="120">
        <el-tag slot-scope="{ row }" size="small" :type="row.isOrdered ? 'primary' : 'info'">{{ row.isOrdered ? '是' : '否' }}</el-tag>
      </el-table-column>
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 巡更路线列表
import auth from '@/plugins/auth'
import { selectProjectExcludeCfProjectStage } from '@/api/baseManage/project'
import { getRoute, removeRoute } from '@/api/cq/patrol'
import EditItem from './components/EditRoute'
export default {
  name: 'PatRouteList',
  components: { EditItem },
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: '',
        routeName: ''
      },
      showEditItem: false,
      prjLoading: false,
      projectList: []
    }
  },
  computed: {
    disableAction() {
      return this.listLoading
    },
    showBtns() {
      // 操作按钮权限
      const permCode = 'cq:patrol:route'
      const edit = auth.hasPermi(`${permCode}:edit`)
      const add = auth.hasPermi(`${permCode}:add`)
      const remove = auth.hasPermi(`${permCode}:remove`)
      const info = auth.hasPermi(`${permCode}:info`)
      const enable = auth.hasPermi(`${permCode}:enable`)
      const disable = auth.hasPermi(`${permCode}:disable`)
      return { edit, add, remove, info, enable, disable }
    }
  },
  created() {
    this.getProjectList()
    this.getList()
  },
  methods: {
    // 获取授权的用户的项目列表
    async getProjectList() {
      this.prjLoading = true
      const res = await selectProjectExcludeCfProjectStage({ needUserId: true }).catch(() => false)
      this.prjLoading = false
      if (res === false) return
      this.projectList = res.data || []
    },
    async getList() {
      this.listLoading = true
      const res = await getRoute(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.routeName}？`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await removeRoute(item.patRouteId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    },
    openAddItem() {
      this.$refs.EditItem.open({ projectId: this.queryParams.projectId })
    },
    openSpotList() {
      // TODO
    }
  }
}
</script>
